package com.sxt.dao;

import com.sxt.common.PageInfo;
import com.sxt.pojo.User;

import java.util.List;
import java.util.Map;

/**
 * @Description: 用户数据操作类
 * @author: Mr.T
 * @date 2020-11-03 11:14
 */
public class UserDao extends BaseDao {

    /**
     * 根据用户名 和 密码查询用户
     * @param username
     * @param password
     * @return
     */
    public User selectUser(String username,String password){
        String sql = "select  `id`, `username`, `password`, `realname`, `role`, `deleted`, `img`, `create_time` as createTime, `modify_time` as modifyTime, `deleted_time` as deletedTime from user where username = ? and password=?";
        User user = super.selectOne(sql, User.class, username, password);
        return user;
    };

    /**
     * 分页查询
     * @param params
     * @param page
     * @param limit
     * @return
     */
    public PageInfo<User> selectPage(Map<String, String> params, String page, String limit) {
        String sql = "select  `id`, `username`, `password`, `realname`, `role`, `deleted`, `img`, `create_time` as createTime, `modify_time` as modifyTime, `deleted_time` as deletedTime from user  where 1=1 ";
        //判断map中是否包含 realname这个key
        if (params.containsKey("realname")){
            sql = sql + " and realname like '%"+params.get("realname")+"%' ";
        }
        //select * from user where 1=1
        if (params.containsKey("role")){
            sql = sql + "  and  role ="+params.get("role");
        }
        if (params.containsKey("deleted")){
            sql = sql + " and deleted ="+params.get("deleted");
        }
        //select * from user where and xxx = xx and  xx =  xx  and  xxx = xx
        //select * form user where
        //判断是否是where 结尾  是where结尾  去掉 where
        //不是where 结尾 去掉 第一个and
        //if
        PageInfo<User> pageInfo = super.selectPage(sql,User.class,page,limit);
        return  pageInfo;
    }

    public void updateState(String id, Integer delete) {
        String sql = "update user set deleted =  ?,deleted_time = now() where id = ?";
        super.update(sql,delete,id);
    }
    /**
     * 修改密码
     * @param id
     * @param password
     */
    public void updatePassword(String id, String password) {
        String sql = "update user set password =  ? where id = ?";
        super.update(sql,password,id);
    }

    /**
     * 根据用户名查询用户信息
     * @param username
     * @return
     */
    public User selectUserByUsername(String username) {
        String sql = "select  `id`, `username`, `password`, `realname`, `role`, `deleted`, `img`, `create_time` as createTime, `modify_time` as modifyTime, `deleted_time` as deletedTime from user  where username = ? ";
        return  super.selectOne(sql,User.class,username);
    }

    /**
     * 新增用户
     * @param username
     * @param password
     * @param realname
     * @param role
     */
    public void insert(String username, String password, String realname, String role) {
        String sql = "insert into user (username,password,realname,role) value(?,?,?,?)";
        super.update(sql,username,password,realname,role);
    }

    public List<User> selectUsersByRole(Integer role,Integer deleted) {
        String sql = "select id,realname from user where role = ? and deleted = ?";
        return  super.selectList(sql,User.class,role,deleted);
    }

    public void updateImg(Integer id, String url) {
        String sql = "update user set img =  ? where id = ?";
        super.update(sql,url,id);
    }
}
